const mongoose = require('mongoose');
const { v4: uuidv4 } = require('uuid');

const roleSchema = new mongoose.Schema({
    id: {
        type: String,
        required: true,
        unique: true,
        default: () => uuidv4(),
        description: 'id'
    },
    name: {
        type: String,
        required: [true, '角色标识必填'],
        unique: true,
        trim: true,
        description: '角色标识'
    },
    cnName: {
        type: String,
        required: [true, '角色中文名称必填'],
        unique: true,
        trim: true,
        description: '角色中文名称'
    },
    role_desc: {
        type: String,
        default: '角色描述',
        description: '角色描述'
    },
    createdAt: {
        type: Date,
        required: true,
        default: Date.now,
        immutable: true,
        description: '创建时间'
    },
    updatedAt: {
        type: Date,
        required: true,
        default: Date.now,
        description: '更新时间'
    }
})
// 更新时自动更新 updatedAt
roleSchema.pre(['updateOne', 'findOneAndUpdate'], function (next) {
    this._update = this._update || {};
    this._update.updatedAt = new Date();
    next();
});

// 创建索引
roleSchema.index({ id: 1 }, { unique: true });

module.exports = mongoose.model('Role', roleSchema); 